package com.ynny.dao;

import com.ynny.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class studentDaoImpl implements StudentDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int addStudent(String number, String name, String clbym, String grade) {
        return 0;
    }

    @Override
    public int insertStudent(String number, String name, String gender, String clbum, String grade) {
        String sql = "insert into students(number,name,gender,clbum,grade) values(?,?,?,?,?)";
        int i = jdbcTemplate.update(sql, number, name, gender, clbum, grade);

        return i;

    }

    @Override
    public int updateStudent(String number, String name, String clbym, String grade) {
        return 0;
    }

    @Override
    public int updateStudent(String newnumber, String newgender, String newclbum, String newgrade, String name) {
        String sql = "update students set number=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql, newnumber, newgender, newclbum, newgrade, name);
        if (i > 0) {
            System.out.println("修改成功");
        }

        return i;
    }

    @Override
    public List<Student> selectAll() {
        String sql = "select * from student";
        List<Student> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));


        return query;
    }

    @Override
    public Student selectStudent(String number) {
        String sql = "select * from student where number=?";
        Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Student.class), number);

        return student;
    }
    @Override
    public int update(String number) {
        String sql = "update student set number=? where number=?";
        int i = jdbcTemplate.update(sql, number, number);
        if (i > 0) {
            System.out.println("修改成功");
        }
        return i;
    }
}
